// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.asm;

import java.util.Set;

import org.objectweb.asm.Label;

/**
 * Collects used labels.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class UsedLabelsCollector extends BaseMethodVisitor {

  private final Set<Label> collector;

  public UsedLabelsCollector(Set<Label> collector) {
    this.collector = collector;
  }

  @Override
  public void visitJumpInsn(int opcode, Label label) {
    collector.add(label);
  }

  @Override
  public void visitLineNumber(int line, Label start) {
    collector.add(start);
  }

  @Override
  public void visitLocalVariable(String name, String desc, String signature, Label start,
      Label end, int index) {
    collector.add(start);
    collector.add(end);
  }

  @Override
  public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
    collector.add(dflt);
    for (int i = 0; i < labels.length; i++) collector.add(labels[i]);
  }

  @Override
  public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
    collector.add(dflt);
    for (int i = 0; i < labels.length; i++) collector.add(labels[i]);
  }

  @Override
  public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
    collector.add(start);
    collector.add(end);
    collector.add(handler);
  }
}
